package com.bole.controller;

import com.bole.dao.TbCarImgDao;
import com.bole.dto.car.TbCarDto;
import com.bole.dto.img.DicImgTypeDto;
import com.bole.dto.img.TbCarImgDto;
import com.bole.pojo.DicImgTypeDo;
import com.bole.pojo.TbCarDo;
import com.bole.pojo.TbCarImgDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("img")
@Api(value = "车辆图片管理")
public class TbCarImgController {
    @Autowired
    private TbCarImgDao imgDao;

    @GetMapping("getImgInfo")
    @ApiOperation(value = "查询图片")
    public List<TbCarImgDto> getImgInfo() {
        List<TbCarImgDo> all = imgDao.findAll();
        ArrayList<TbCarImgDto> carImgDtoArrayList = new ArrayList<>();
        for (TbCarImgDo carImgDo : all) {
            TbCarImgDto carImgDto = new TbCarImgDto();
            BeanUtils.copyProperties(carImgDo,carImgDto);
            carImgDtoArrayList.add(carImgDto);
        }
        return carImgDtoArrayList;

    }


    @PostMapping("saveImgInfo")
    @ApiOperation(value = "保存图片")
    @ApiImplicitParam(name = "dicImgTypeDo",value = "图片对象",required = true,dataTypeClass = TbCarImgDto.class)
    public void saveContentCateInfo(@RequestBody TbCarImgDo carImgDo) {
       TbCarImgDo imgDo = imgDao.save(carImgDo);
       TbCarImgDto carImgDto = new TbCarImgDto();
        BeanUtils.copyProperties(imgDo,carImgDo);
    }


    @PostMapping("toUpdate")
    @ApiOperation(value = "图片回显")
    @ApiImplicitParam(name = "id",value = "图片id",required = true,dataTypeClass = Long.class)
    public TbCarImgDto toUpdate(@RequestParam("id") Long id) {
        TbCarImgDo carImgDo = imgDao.findById(id).get();
       TbCarImgDto imgDto = new TbCarImgDto();
        BeanUtils.copyProperties(carImgDo,imgDto);
        return imgDto;
    }

    @PostMapping("updateImgInfo")
    @ApiOperation(value = "修改图片信息")
    @ApiImplicitParam(name = "dicImgTypeDo",value = "类型对象",required = true,dataTypeClass = DicImgTypeDo.class)
    public void updateContentCateInfo(@RequestBody TbCarImgDo carImgDo) {
       TbCarImgDo carImgDo1 = imgDao.save(carImgDo);
       TbCarImgDto carImgDto = new TbCarImgDto();
        BeanUtils.copyProperties(carImgDo1,carImgDto);
    }

    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除广告类型",notes = "批量删除广告类型")
    @ApiImplicitParam(name = "idss",value = "品牌对象",required = true,dataTypeClass = Long.class)
    public void deleteBatch(@RequestParam("idss") List<Long> idss) {

        imgDao.deleteBatch(idss);
    }


    //根据carId查找
    @PostMapping("getCarImgById")
    @ApiOperation(value = "通过汽车id查找")
    @ApiImplicitParam(name = "carId",value = "通过汽车id查找",required = true,dataTypeClass = Long.class)
    public TbCarImgDto getCarImgById(@RequestParam("carId") Long carId) {
        TbCarImgDo tbCarImgDo= imgDao.getCarImgById(carId);
        TbCarImgDto carImgDto = new TbCarImgDto();
        BeanUtils.copyProperties(tbCarImgDo,carImgDto);
        return carImgDto;
    }

    //根据carId查找
    @PostMapping("findByCarId")
    @ApiOperation(value = "通过汽车id查找")
    @ApiImplicitParam(name = "carId",value = "通过汽车id查找",required = true,dataTypeClass = Long.class)
    public TbCarImgDto findByCarId(@RequestParam("carId") Long carId) {
        TbCarImgDo tbCarImgDo= imgDao.findByCarId(carId);
        TbCarImgDto carImgDto = new TbCarImgDto();
        BeanUtils.copyProperties(tbCarImgDo,carImgDto);
        return carImgDto;
    }


}
